#include "EbnWorldStateManager.h"

CEbnWorldStateManager::CEbnWorldStateManager(void)
{
}

CEbnWorldStateManager::~CEbnWorldStateManager(void)
{
    clear();
}

void CEbnWorldStateManager::clear()
{
    vector<CEbnWorldState *>::iterator itWorldState;
    CEbnWorldState * pWorldState = NULL;

    for (itWorldState = mWorldStates.begin(); itWorldState != mWorldStates.end(); itWorldState++)
    {
        pWorldState = (* itWorldState);

        delete pWorldState;
        pWorldState = NULL;
    }

    mWorldStates.clear();
}

CEbnWorldState * CEbnWorldStateManager::addWorldState(string worldStateName)
{
    CEbnWorldState * pWorldState = getWorldState(worldStateName);

    if (NULL == pWorldState)
    {
        pWorldState = new CEbnWorldState(worldStateName, false);
        mWorldStates.push_back(pWorldState);
    }

    return pWorldState;
}

void CEbnWorldStateManager::removeWorldState(CEbnWorldState * worldState)
{
    vector<CEbnWorldState *>::iterator itWorldState;
    CEbnWorldState * pWorldState = NULL;

    for (vector<CEbnWorldState *>::iterator it = mWorldStates.begin(); it != mWorldStates.end(); it++)
    {
        if (worldState == (* it))
        {
            itWorldState = it;
        }
    }

    pWorldState = (*itWorldState);

    if (pWorldState)
    {
        delete pWorldState;
        pWorldState = NULL;

        mWorldStates.erase(itWorldState);
    }
}

void CEbnWorldStateManager::removeWorldState(string worldStateName)
{
    CEbnWorldState * pWorldState = NULL;

    pWorldState = getWorldState(worldStateName);
    removeWorldState(pWorldState);
}

CEbnWorldState * CEbnWorldStateManager::getWorldState(string worldStateName)
{
    vector<CEbnWorldState *>::iterator itWorldState;
    for (itWorldState = mWorldStates.begin(); itWorldState != mWorldStates.end(); itWorldState++)
    {
        if (0 == (*itWorldState)->getName().compare(worldStateName))
        {
            return (*itWorldState);
        }
    }

    return NULL;

}

bool CEbnWorldStateManager::hasProposition(string worldStateName)
{
    CEbnWorldState * pWorldState = getWorldState(worldStateName);

    if (NULL == pWorldState)
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool CEbnWorldStateManager::setWorldStateValue(string worldStateName, float value)
{
    CEbnWorldState * pWorldState = getWorldState(worldStateName);

    if (NULL == pWorldState)
    {
        return false;
    }
    else
    {
        pWorldState->setTruthValue(value);
        return true;
    }
}

float CEbnWorldStateManager::getWorldStateValue(string worldStateName)
{
    CEbnWorldState * pWorldState = getWorldState(worldStateName);

    if (NULL == pWorldState)
    {
        return 0;
    }
    else
    {
        return pWorldState->getTruthValue();
    }

}
